# Create your views here.
from eventr.main.models import *
from django import oldforms as forms
from django.newforms.models import form_for_model
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse


from django.utils.translation import gettext_lazy as _

fc = form_for_model(Event)
class f2(fc):
	def clean_date_end(self,value):
		if 'date_start' in self.cleaned_data and 'date_end' in self.cleaned_data:
			if self.cleaned_data['date_start'] >= self.cleaned_data['date_end']:
				return self.cleaned_data['date_end']
		raise forms.ValidationError(_('End date should be bigger than start date'))
def get_base_context(request):
	from django.conf import settings
	menu = [
		{
			'url' : '/' , 
			'label' : _('main')
		},
		{
			'url' : '/list/', 
			'label' : _('event list')
		},
		{
			'url' : '/map/', 
			'label' : _('event map')
		},
		{
			'url' : '/add/', 
			'label' : _('add')
		}
	]
	base_context = {
		'menu' : menu,
		'MAPS_API_KEY' : settings.MAPS_API_KEY
	}
	for k, v in enumerate(menu):
		if v['url'] == request.path:
			menu[k]['selected'] = True
			base_context['title'] = menu[k]['label']
	return base_context
	
def show(request, ev_id):
    ev = get_object_or_404(Event,pk=ev_id)
    context = get_base_context(request)
    context['event'] = ev
    return render_to_response('details.html',context)

def list_events(request):
    events = Event.objects.all()
    context = get_base_context(request)
    context['event_list'] = events
    return render_to_response('list.html',context)
    
def add_event(request):
	context = get_base_context(request)
	if request.method == 'POST':
		f = f2(request.POST)
		if f.is_valid():
			new_ev = f.save()
			return HttpResponseRedirect(new_ev.get_absolute_url())
		else:
			context['f'] = f
			context['errors'] = f.errors
			return render_to_response('add_form.html',context)
	else:
		f = form_for_model(Event)()
		context['f'] = f
		return render_to_response('add_form.html',context)

def edit_event(request,ev_id):
	context = get_base_context(request)
	ev = get_object_or_404(Event,pk=ev_id)
	if request.method == 'POST':
		f = f2(request.POST)
		if f.is_valid():
			new_ev = f.save()
			return HttpResponseRedirect(new_ev.get_absolute_url())
		else:
			context['f'] = f
			context['errors'] = f.errors
    			return render_to_response('add_form.html',context)
	else:
		# FIXME: POST object should be here instead of model
		context['f'] = form_for_model(Event)(ev)
		return render_to_response('add_form.html',context)

	
def event_map(request):
	events = Event.objects.all()
	lats = [e.lat for e in events]
	lngs = [e.lng for e in events]
	bounds = {
		'lat_max':max(lats),
		'lat_min':min(lats),
		'lng_max':max(lngs),
		'lng_min':min(lngs),
	}
	
	context = get_base_context(request)
	context['events'] = events
	context['b'] = bounds
	return render_to_response('map.html',context)
	
def index(request):
	return render_to_response('index.html', get_base_context(request))
